package com.shanxi.cz.module.biz.service.ammeterrecordyear;

import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordyear.vo.AmmeterRecordYearPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordyear.vo.AmmeterRecordYearSaveReqVO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordyear.AmmeterRecordYearDO;
import com.shanxi.cz.module.biz.dal.mysql.ammeterrecordyear.AmmeterRecordYearMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.shanxi.cz.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.shanxi.cz.module.biz.enums.ErrorCodeConstants.AMMETER_RECORD_YEAR_NOT_EXISTS;

/**
 * 电表数据年报 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class AmmeterRecordYearServiceImpl implements AmmeterRecordYearService {

    @Resource
    private AmmeterRecordYearMapper ammeterRecordYearMapper;

    @Override
    public Integer createAmmeterRecordYear(AmmeterRecordYearSaveReqVO createReqVO) {
        // 插入
        AmmeterRecordYearDO ammeterRecordYear = BeanUtils.toBean(createReqVO, AmmeterRecordYearDO.class);
        ammeterRecordYearMapper.insert(ammeterRecordYear);

        // 返回
        return ammeterRecordYear.getId();
    }

    @Override
    public void updateAmmeterRecordYear(AmmeterRecordYearSaveReqVO updateReqVO) {
        // 校验存在
        validateAmmeterRecordYearExists(updateReqVO.getId());
        // 更新
        AmmeterRecordYearDO updateObj = BeanUtils.toBean(updateReqVO, AmmeterRecordYearDO.class);
        ammeterRecordYearMapper.updateById(updateObj);
    }

    @Override
    public void deleteAmmeterRecordYear(Integer id) {
        // 校验存在
        validateAmmeterRecordYearExists(id);
        // 删除
        ammeterRecordYearMapper.deleteById(id);
    }

    @Override
    public void deleteAmmeterRecordYearListByIds(List<Integer> ids) {
        // 删除
        ammeterRecordYearMapper.deleteByIds(ids);
    }


    private void validateAmmeterRecordYearExists(Integer id) {
        if (ammeterRecordYearMapper.selectById(id) == null) {
            throw exception(AMMETER_RECORD_YEAR_NOT_EXISTS);
        }
    }

    @Override
    public AmmeterRecordYearDO getAmmeterRecordYear(Integer id) {
        return ammeterRecordYearMapper.selectById(id);
    }

    @Override
    public AmmeterRecordYearDO getAmmeterRecordYear(Integer ammeterId, String recordYear) {
        return ammeterRecordYearMapper.selectFirstOne(AmmeterRecordYearDO::getAmmeterId, ammeterId, AmmeterRecordYearDO::getRecordYear, recordYear);
    }

    @Override
    public PageResult<AmmeterRecordYearDO> getAmmeterRecordYearPage(AmmeterRecordYearPageReqVO pageReqVO) {
        return ammeterRecordYearMapper.selectPage(pageReqVO);
    }

}